// pages/modifyPw/modifyPw.js
const http=require('../../utils/http')

Page({

  /**
   * Page initial data
   */
  data: {
      oldPw:"",
      newPw:"",
      newPwAgrin:""
  },

  bindOldPw(e){
    this.setData({
      oldPw:e.detail.value
   })
  },

  bindNewPw(e){
     this.setData({
        newPw:e.detail.value
     })
  },

  bindAgrinNewPw(e){
    this.setData({
      newPwAgrin:e.detail.value
   })
  },
  
  submit(){
     if(this.data.oldPw.length==0){
       wx.showToast({
         title: '新输入原密码',
         icon:'none'
       })
       return
     }
     if(this.data.newPw.length==0){
      wx.showToast({
        title: '新输入新密码',
        icon:'none'
      })
      return
    }
    if(this.data.newPwAgrin.length==0){
      wx.showToast({
        title: '新再次输入原密码',
        icon:'none'
      })
      return
    }

    if(this.data.newPwAgrin!=this.data.newPw){
      wx.showToast({
        title: '两次密码输入不一致!',
        icon:'none'
      })
      return
    }
  
    wx.showLoading({
      title: '请稍等...',
    })
    let params={
      'old_pwd':this.data.oldPw,
      'pwd_new':this.data.newPw
    }
    http.postData('api/sys/user/change_password',params)
    .then((value)=>{
       wx.hideLoading()
       wx.showToast({
         title: value['msg'],
         icon:'none'
       })
       if(value['code']==0){
         wx.reLaunch({
           url: '../login/login',
         })
       }
    })
    .catch((err)=>{
      wx.hideLoading()
      wx.showToast({
        title: err['msg'],
        icon:'none'
      })
    })

  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {

  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  }
})